package com.ccjt.eip.web.dao.impl;

import java.util.*;
import static com.ccjt.eip.web.commons.JDBC.jdbc;
import org.springframework.stereotype.Service;
import java.util.List;
import com.ccjt.eip.web.vo.${voEntity};
import com.ccjt.eip.web.services.I${voEntity}Dao;
import com.ccjt.eip.web.commons.SqlUtil;
import com.ccjt.eip.web.vo.ResultDto;
import org.springframework.stereotype.Component;

/**
 *
 * @author
 * @date ${creatTime}
 */
@Component
public class ${voEntity}DaoImpl implements I${voEntity}Dao {

    @Override
    public Map<String,Object> list(${voEntity} ${voEntityLowCase},Integer page,Integer rows){
        Map<String, Object> map = new HashMap<>();
        List<Object> params = new ArrayList<Object>();
        try {
            String sql = "select " + SqlUtil.selectPart(${voEntity}.class, "${tableName}") + " from ${tableName} where isdelete = 0 ";
            if (page != null) {
                List<${voEntity}> ${voEntityLowCase}s = jdbc.beanList(sql, page, rows, ${voEntity}.class, params.toArray());
                int total = jdbc.getCount(sql, params.toArray());
                map.put("rows", ${voEntityLowCase}s);
                map.put("total", total);
            } else {
                List<${voEntity}> ${voEntityLowCase}s = jdbc.beanList(sql, ${voEntity}.class, params.toArray());
                int total = jdbc.getCount(sql, params.toArray());
                map.put("rows", ${voEntityLowCase}s);
                map.put("total", total);
            }
            return map;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return null;
    }

    @Override
    public ${voEntity} get${voEntity}ById(int id){
        try {
            String sql = "select " + SqlUtil.selectPart(${voEntity}.class, "${tableName}") + " from ${tableName} where isdelete = 0 and id = ?";
            ${voEntity} ${voEntityLowCase} = jdbc.bean(sql, ${voEntity}.class, id);
            return ${voEntityLowCase};
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ResultDto saveOrUpdate${voEntity}(${voEntity} ${voEntityLowCase}){
        if(${voEntityLowCase}.getId() == null){
            jdbc.insertBean(${voEntityLowCase});
        }else{
            jdbc.updateBean(${voEntityLowCase});
        }
        return new ResultDto(RetCode.SUCCESS,"发布成功");
    }

    @Override
    public ResultDto delete${voEntity}ByIds(String[] ids){
        try {
            ResultDto resultDto = jdbc.execute(new DBTransAction<ResultDto>() {
                @Override
                public ResultDto action(Connection conn) throws Exception {
                    try {
                        for (int i = 0; i < ids.length; i++) {
                            String sql = "update ${tableName} set update_time = now(),isdelete = 1 where id = ? ";
                            jdbc.executeExt(conn,sql,ids[i]);
                        }
                        return new ResultDto(RetCode.SUCCESS,"删除成功");
                    } catch (Exception e) {
                        throw e;
                    }
                }
            });
            return resultDto;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDto(RetCode.Failed,"删除失败!");
    }

}
